// pages/home/alarmList/alarmList.js
var e = getApp(), t = getApp().getAccessToken, a = require("../../../utils/formate.js");
require("../../../utils/moment.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    deviceAlarms: [],
    pageSize: 5,
    currentPage: 1,
    imei: "",
    deviceName: "",
    isHiddenEmptyContent: !0,
    isShowNoMoreContent: !1,
    baseColor: e.system.color,
    image_prefix: e.system.image_prefix
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that = this, a = e.imei, i = e.deviceName;
    this.setData({
      imei: a,
      deviceName: i,
      currentPage: 1
    }, function () {
      wx.showLoading(), 
      that.getDeviceAlarmList();
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    this.setData({
      currentPage: 1
    }, function () {
      that.getDeviceAlarmList(), 
      wx.stopPullDownRefresh();
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (e) {
    var page = this.data.currentPage, that = this;
    this.setData({
      currentPage: ++page
    }, function () {
      that.getDeviceAlarmList();
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  _getDeviceAlarmList: function () {
    var t = this, i = this.data, n = i.imei, o = i.pageSize, s = i.currentPage, n = i.imei, c = i.deviceName, r = i.deviceAlarms, m = i.isHiddenEmptyContent;
    let openId = wx.getStorageSync("wechatOpenid")
    wx.request({
      url: e.system.app_url_prefix + "/weapp/userinfo/alarm/query?openId=" + openId + "&vehicleId=" + n,
      data: {
        pageSize: o,
        pageNum: s,
      },
      method: "POST",
      header: {
        Accept: "application/json",
        "Content-Type": "application/json; charset=utf-8"
      },
      success: function (e) {
        let code = parseInt(e.data.code)
        if(code == 0){
          let list = e.data.list
          list.forEach(function (item, t) {
            item.deviceName = c
          });

          var i = 1 === s ? list : r.concat(list);
          0 === i.length ? m = !1 : i.length > 0 && (m = !0), t.setData({
            deviceAlarms: i,
            isShowNoMoreContent: 0 !== i.length && list.length < o,
            isHiddenEmptyContent: m
          });
        }
      },
      fail: function (e) { },
      complete: function () {
        wx.hideLoading();
      }
    });
  },
  
  getDeviceAlarmList: function (e) {
    this._getDeviceAlarmList()
  }
})